#!/bin/bash
#先创建网络
#docker network create --subnet=172.26.0.0/16 --ipv6 --subnet=2402:4e00:1700:810a:6666:0001:0001:0000/112 --driver bridge bright
docker network create --subnet=172.26.0.0/16 --ipv6 --subnet=fd19:82::/112 --driver bridge bright
# 运行前检测
if [ -z "$1" ] 
then
    echo "Please input the app name."
    exit 1
fi

# if [[ ! -f .env ]]
# then
#    echo '.env not found'
#    exit 0 
# fi

set -o allexport
source .env.example
if [[ -f .env ]]
then
   source .env
fi
set +o allexport

if [ -z "$NGINX_HOSTNAME" ]
then
    export NGINX_HOSTNAME=`hostname`
fi

phymem=$(awk '/MemTotal/ { printf "%d", $2/1024 }' /proc/meminfo)
printf "%-21s%-10s\n" "Hostname:" ${NGINX_HOSTNAME}
printf "%-21s%-10s\n" "Server RAM:" ${phymem}M
if [[ $phymem -gt 16000 ]]
then
    phymem=16000
fi
if [[ $phymem -gt 8000 ]]
then
    phymem=$(echo "$phymem" | awk '{printf "%d",$1*2/3}')
fi
printf "%-21s%-10s\n" "Allocated RAM:" ${phymem}M

mem_half=$(echo "$phymem" | awk '{printf "%d",$1/2}')
mem_one_fifth=$(echo "$phymem" | awk '{printf "%d",$1/5}')

if [ -z "$NGINX_MAX_MEMORY" ]
then
    if [[ $phymem -gt 2000 ]]
    then
        export NGINX_MAX_MEMORY=${mem_one_fifth}M
    else
        export NGINX_MAX_MEMORY=${mem_half}M
    fi
fi
printf "%-21s%-10s\n" "NGINX_MAX_MEMORY:" $NGINX_MAX_MEMORY

if [ -z "$PHP_FPM_MAX_MEMORY" ]
then
    export PHP_FPM_MAX_MEMORY=${mem_half}M
fi
printf "%-21s%-10s\n" "PHP_FPM_MAX_MEMORY:" $PHP_FPM_MAX_MEMORY

if [ -z "$PHP8_FPM_MAX_MEMORY" ]
then
    export PHP8_FPM_MAX_MEMORY=${mem_half}M
fi
printf "%-21s%-10s\n" "PHP8_FPM_MAX_MEMORY:" $PHP8_FPM_MAX_MEMORY

if [ -z "$REDIS_MAX_MEMORY" ]
then
    export REDIS_MAX_MEMORY=${mem_one_fifth}M
fi
printf "%-21s%-10s\n" "REDIS_MAX_MEMORY:" $REDIS_MAX_MEMORY

if [ -z "$SSDB_MAX_MEMORY" ]
then
    export SSDB_MAX_MEMORY=${mem_one_fifth}M
fi
printf "%-21s%-10s\n" "SSDB_MAX_MEMORY:" $SSDB_MAX_MEMORY

#存储目录
if [[ ! -d /data/ ]]
then
    mkdir /data/
fi

# 初始化
if [[ ! -f /usr/local/sbin/nginx ]]
then
    cat <<EOF > /usr/local/sbin/nginx
#!/bin/bash
/usr/bin/docker exec -it nginx nginx \$@
EOF
    chmod +x /usr/local/sbin/nginx
fi

if [[ ! -d /var/log/ngx-openresty/ ]]
then
    ln -s $(pwd)/nginx/log/ /var/log/ngx-openresty
fi

if [[ ! -f ./nginx/sites/default.conf ]]
then
    cp ./nginx/sites/default.conf.example ./nginx/sites/default.conf
fi

if [[ ! -f ./nginx/nginx.conf ]]
then
    cp ./nginx/nginx.conf.example ./nginx/nginx.conf
fi

if [[ ! -d /opt/www/ ]]
then
    mkdir  -p /opt/www/
fi

if [[ ! -d /opt/www/default ]]
then
    cp -r ./code/default /opt/www/
fi

if [[ ! -f /opt/www/default/100MB-$NGINX_HOSTNAME.bin ]]
then
    wget https://ash-speed.hetzner.com/100MB.bin -O /opt/www/default/100MB-$NGINX_HOSTNAME.bin
fi

if [[ ! -d /etc/cert/ ]]
then
    mkdir  -p /etc/cert/
fi

if [[ ! -f ./php/7/php.ini ]]
then
    cp ./php/7/php.ini.example ./php/7/php.ini
fi

if [[ ! -f ./php/8/php.ini ]]
then
    cp ./php/8/php.ini.example ./php/8/php.ini
fi

if [[ ! -f ./php/7/php-fpm.d/www.conf ]]
then
    cp ./php/7/php-fpm.d/www.conf.example ./php/7/php-fpm.d/www.conf
fi

if [[ ! -f ./php/8/php-fpm.d/www.conf ]]
then
    cp ./php/8/php-fpm.d/www.conf.example ./php/8/php-fpm.d/www.conf
fi

if [[ ! -f ./phpmyadmin/config.user.inc.php ]]
then
    cp ./phpmyadmin/config.user.inc.php.example ./phpmyadmin/config.user.inc.php
fi

if [[ ! -f ./redis/redis.conf ]]
then
    cp ./redis/redis.conf.example ./redis/redis.conf
fi

if [[ ! -f ./ssdb/ssdb.conf ]]
then
    cp ./ssdb/ssdb.conf.example ./ssdb/ssdb.conf
fi

# 部署服务
composefile="docker-compose.$1.yml"
if [ ! -f $composefile ]; then
    echo "$composefile file not found."
    exit 1
fi
docker compose -f $composefile up -d